Image to PDF

🖼️ Image to PDF

Description

img2pdf is an open source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance image (Brightness, contrast and other things)


Installation

You will need img2pdf package installed, you can install it by running the following command

pip install img2pdf

Steps To Execution

  • Fork this repo and navigate to IMAGES & PHOTO SCRIPTS folder
  • (optional) Add images to same directory with this img2pdf.py.
  • Modify the names in img2pdf.py
  • Run this img2pdf.py $ python img2pdf.py
  • The output directory will now include a brand-new PDF file.

Output

Image contains an alpha channel. Computing a separate soft mask (/SMask) image to store transparency in PDF.
Successfully made pdf file


Source Code: image2pdf.py

 # importing necessary libraries
import img2pdf
from PIL import Image
import os
  
# storing image path
img_path = "C:/Users/vedan/OneDrive/Desktop/image.png"
  
# storing pdf path
pdf_path = "C:/Users/vedan/OneDrive/Desktop/image.pdf"
  
# opening image
image = Image.open(img_path)
  
# converting into chunks using img2pdf
pdf_bytes = img2pdf.convert(image.filename)
  
# opening or creating pdf file
file = open(pdf_path, "wb")
  
# writing pdf files with chunks
file.write(pdf_bytes)
  
# closing image file
image.close()
  
# closing pdf file
file.close()
  
# output
print("Successfully made pdf file")